﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;



using OF.Component.Model;
using OF.Utility;
using OF.Utility.Logging;
using System.Text;
using System.Web;
using System.Xml.XPath;
using CWQ.Api.Common;
using CWQ.Api.Models;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OF.AppContext;
using OF.Component.Base;
using OF.Component.BLL;
using OF.Component.DTO;
using Swashbuckle.Swagger.Annotations;



namespace CWQ.Api.Controllers
{
    /// <summary>
    /// 通用控制器
    /// </summary>
    public class CarCircleController : BaseApiController
    {

        /// <summary>
        /// 提交 我要发单 请求
        /// </summary>
        /// <param name="request">发单请求</param>
        /// <returns></returns>
        public ResultData<ResponseCreateSendOrder> CreateSendOrder(RequestCreateSendOrder request)
        {
            var result = ResultData<ResponseCreateSendOrder>.Error("");
            var oModel = new PostSendInfo();

            request.MergeTo(oModel);
            oModel.ShopID = UserContext.Current.UserID;
            oModel.CreatedDate = DateTime.Now;
            oModel.IsActive = 1;
            int nReturn = PostSendOperate.Insert(oModel);  
            if (nReturn > 0)
            {
                if (request.PostImgs != null && request.PostImgs.Count > 0)
                {
                    var list = request.PostImgs;
                    list.ForEach((item) =>
                    {
                        var oImgInfo = new PostImgInfo()
                        {
                            PostType = 1,//1：发单动态，2：接单动态
                            PostID = nReturn,
                            ImgUrl = item
                        };
                        PostImgOperate.Insert(oImgInfo);
                    });
                }
                result = ResultData<ResponseCreateSendOrder>.Success(new ResponseCreateSendOrder()
                {
                    PostID = nReturn
                });
            }
            return result;
        }

        /// <summary>
        /// 提交 我要接单 请求
        /// </summary>
        /// <param name="request">接单请求</param>
        /// <returns></returns>
        public ResultData<ResponseCreateReceiveOrder> CreateReceiveOrder(RequestCreateReceiveOrder request)
        {
            var result = ResultData<ResponseCreateReceiveOrder>.Error("");
            var oModel = new PostReceiveInfo();

            request.MergeTo(oModel);
            oModel.ShopID = UserContext.Current.UserID;
            oModel.CreatedDate = DateTime.Now;
            oModel.IsActive = 1;
            int nReturn = PostReceiveOperate.Insert(oModel);
            if (nReturn > 0)
            {
                if (request.PostImgs != null && request.PostImgs.Count > 0)
                {
                    var list = request.PostImgs;
                    list.ForEach((item) =>
                    {
                        var oImgInfo = new PostImgInfo()
                        {
                            PostType = 2,//1：发单动态，2：接单动态
                            PostID = nReturn,
                            ImgUrl = item
                        };
                        PostImgOperate.Insert(oImgInfo);
                    });
                }
                result = ResultData<ResponseCreateReceiveOrder>.Success(new ResponseCreateReceiveOrder()
                {
                    PostID = nReturn
                });
            }
            return result;
        }

        /// <summary>
        /// 车务圈动态
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<MyPostModel> SearchMyPost(int shopId, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            MyPostModel myPost = new MyPostModel();
            var result = ResultData<MyPostModel>.Success("");
            List<CarCircleInfoDto> list = PostSendOperate.SearchPostList(shopId, pageIndex, pageSize, out totalRecord);
            myPost.List = list;
            myPost.Total = totalRecord;
            result.data = myPost;
            return result;
        }

        /// <summary>
        /// 查询动态详情
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="postType"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultData<PostDetailDto> SearchPostById(int postId,int postType)
        {
            var result = ResultData<PostDetailDto>.Success("");
            PostDetailDto info = null;
            if (postType == 1)
            {
                info = PostSendOperate.SearchPostSendDetail(postId);
            }
            else if (postType == 2)
            {
                info = PostReceiveOperate.SearchPostSendDetail(postId);
            }
            if (info != null)
            {
                IList<PostImgInfo> imgList = PostImgOperate.QueryList(a => a.PostID == info.PostID&&a.PostType==info.PostType);
                imgList.ForEach(a =>
                {
                    a.ImgUrl = GetHttpFilePath(a.ImgUrl);
                });
                info.HeadImgSrc = GetHttpFilePath(info.HeadImgSrc);
                info.PostImgList = imgList.Select(a => a.ImgUrl).ToList();
                info.LikeUser = UserDataStatisticsOperate.GetPostLikeShopInfoByPostIdAndDataType(postId, postType);
                info.CommentInfo = CommentOperate.GetPostCommentShopInfoByPostIdAndDataType(postId, postType);
                result.data = info;
            }
            else
            {
                result.status = -1;
                result.message = "查不到数据";
            }
            return result;
        }

        /// <summary>
        /// 车务圈热门动态搜索
        /// </summary>
        /// <param name="con"></param>
        /// <returns></returns>
        public ResultData<ResponseGetSearchHot> GetSearchHot([FromUri]scPost con)
        {
            var result = ResultData<ResponseGetSearchHot>.Error("");
            if (con.rows == 0)
            {
                con.rows = 100;
            }
            if (con.page == 0)
            {
                con.page = 1;
            }
            int total;
            var list = PostOperate.SearchHotList(con, out total);
            list.ForEach(m => m.ComputeFullPostDetail());
            result = ResultData<ResponseGetSearchHot>.Success(new ResponseGetSearchHot
            {
                items = list,
                total = total
            });
            return result;
        }

        /// <summary>
        /// 动态搜索
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public ResultData<ResponseGetSearchList> GetSearchListByCondition([FromUri]scPostCondition condition)
        {
            var result = ResultData<ResponseGetSearchList>.Error("");
            if (condition.rows == 0)
            {
                condition.rows = 100;
            }
            if (condition.page == 0)
            {
                condition.page = 1;
            }
            int total;
            var list = PostOperate.SearchListByCondition(condition, out total);
            list.ForEach(m => m.ComputeFullPostDetail());
            result = ResultData<ResponseGetSearchList>.Success(new ResponseGetSearchList
            {
                items = list,
                total = total
            });
            return result;
        }
    }
}
